<template>
  <div class="content-container">
    <a-row class="select-date-line">
      <a-date-picker
        class="select-item"
        @change="onChange"
        :format="dateFormat"
      />
      <a-select
        class="select-item"
        default-value=""
        style="width: 120px"
        @change="handleChange"
      >
        <a-select-option
          v-for="item in linkList"
          :value="item.starlinkId"
          :key="item.starlinkId"
        >
          {{ item.starlinkId }}
        </a-select-option>
      </a-select>
      <a-button class="select-item" @click="postSearch" type="primary">
        筛选
      </a-button>
      <a-button @click="downloadData" class="select-item" type="primary">
        下载日志
      </a-button>
    </a-row>
    <a-row class="select-date-line">
      <a-radio-group :value="currentType" class="btn-line">
        <a-radio-button
          v-for="(item, index) in typeList"
          :key="index"
          @click="changeModule(item.type)"
          :value="item.type"
        >
          {{ item.label }}
        </a-radio-button>
      </a-radio-group>
      <a-table
        :columns="columns"
        :data-source="allData"
        size="middle"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        :rowKey="(record) => record.taskId"
      >
        <span slot="itemIndex" slot-scope="text, tags, i">{{ i + 1 }}</span>
        <span slot="type" slot-scope="type">
          {{ type | deviceTransfer }}
        </span>
        <a slot="taskId" slot-scope="taskId">{{ taskId }}</a>
        <span slot="operation" slot-scope="text, tags">
          <a @click="showModal(tags.taskId)">查看详情</a>
        </span>
      </a-table>
    </a-row>
  </div>
</template>

<script>
import {
  getStarlinkList,
  getHistoryStatus,
  getDownloadHistory,
} from '../../services/user';
export default {
  data() {
    return {
      dateFormat: 'YYYY/MM/DD',
      linkList: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
      currentType: '65281',
      typeList: [
        {
          label: '底座模块（红网）',
          type: '65281',
        },
        {
          label: '底座模块（蓝网）',
          type: '65282',
        },
        {
          label: '主机模块（主）',
          type: '65290',
        },
        {
          label: '主机模块（备）',
          type: '65292',
        },
        {
          label: '电源板模块',
          type: '65288',
        },
        {
          label: '电源板二模块',
          type: '65289',
        },
        {
          label: '传感器模块',
          type: '65287',
        },
        {
          label: '智能灯光模块',
          type: '65286',
        },
        {
          label: '大算力模块',
          type: '65285',
        },
      ],
      loading: false,
      postData: {
        starlinkId: '',
        start: window.localStorage.getItem('startTime'),
        end: window.localStorage.getItem('endTime'),
      },
      columnsList: {
        red_dizuo: [
          {
            title: '通控版本',
            dataIndex: 'version',
          },
          {
            title: '通控与安全主机1通信状态',
            dataIndex: 'status_anquan_1',
          },
          {
            title: '通控与安全主机2通信状态',
            dataIndex: 'status_anquan_2',
          },
          {
            title: '通控与大算力主机通信状态',
            dataIndex: 'status_dasuanli',
          },
          {
            title: '通控与灯光状态通信状态',
            dataIndex: 'status_dengguang',
          },
          {
            title: '通控与传感器状态通信状态',
            dataIndex: 'status_sensor',
          },
          {
            title: 'cpu使用率',
            dataIndex: 'cpu_utility',
          },
          {
            title: '延迟',
            dataIndex: 'delay',
          },
          {
            title: '最近上传时间',
            dataIndex: 'last_update_time',
          },
        ],

        main_anquan: [
          {
            title: '序列号',
            dataIndex: 'serial_number',
          },
          {
            title: '激光雷达1故障',
            dataIndex: 'radar_1_status',
          },
          {
            title: '激光雷达2故障',
            dataIndex: 'radar_2_status',
          },
          {
            title: '双雷达比较异常',
            dataIndex: 'compare_error',
          },
          {
            title: '位姿检查异常',
            dataIndex: 'posture_error',
          },
          {
            title: '通信异常',
            dataIndex: 'communication_error',
          },
          {
            title: '红网信号平台软件md5',
            dataIndex: 'signal_platform_software_red_md5',
          },
        ],
        backup_anquan: [],
        sensor: [
          {
            title: '工作状态',
            dataIndex: 'status',
          },
          {
            title: '温度',
            dataIndex: 'temperture',
          },
          {
            title: '湿度',
            dataIndex: 'humidity',
          },
          {
            title: 'PM2.5',
            dataIndex: 'pm2.5',
          },
          {
            title: 'PM10',
            dataIndex: 'pm10',
          },
          {
            title: 'CO2',
            dataIndex: 'co2',
          },
          {
            title: 'TVOC',
            dataIndex: 'tvoc',
          },
          {
            title: 'CO',
            dataIndex: 'co',
          },
          {
            title: '氧气',
            dataIndex: 'o2',
          },
          {
            title: '烟',
            dataIndex: 'smoke',
          },
          {
            title: '可燃',
            dataIndex: 'burning',
          },
          {
            title: '最近上传时间',
            dataIndex: 'last_update_time',
          },
        ],
        dengguang: [
          {
            title: '工作状态',
            dataIndex: 'status',
          },
          {
            title: '照明灯光回检',
            dataIndex: 'light_check',
          },
          {
            title: '1路灯光电流',
            dataIndex: 'light_1_current',
          },
          {
            title: ' 2路灯光电流',
            dataIndex: 'light_2_current',
          },
          {
            title: '3路灯光电流',
            dataIndex: 'light_3_current',
          },
          {
            title: '4路灯光电流',
            dataIndex: 'light_4_current',
          },
          {
            title: '5路灯光电流',
            dataIndex: 'light_5_current',
          },
          {
            title: '6路灯光电流',
            dataIndex: 'light_6_current',
          },
          {
            title: '7路灯光电流',
            dataIndex: 'light_7_current',
          },
          {
            title: '电源电压',
            dataIndex: 'voltage',
          },
        ],
        dasuanli: [
          {
            title: 'cpu温度',
            dataIndex: 'cpu_tem',
          },
          {
            title: 'gpu温度',
            dataIndex: 'gpu_tem',
          },
          {
            title: 'cpu使用率',
            dataIndex: 'cpu_utility',
          },
          {
            title: ' 内存使用率',
            dataIndex: 'mem_utility',
          },
          {
            title: '故障码',
            dataIndex: 'error_code',
          },
          {
            title: '最近上传时间',
            dataIndex: 'last_update_time',
          },
        ],
      },

      allData: [],
    };
  },
  computed: {
    columns() {
      let arr = [];
      // 红蓝一致
      // 主备一致
      switch (this.currentType) {
        case '65281':
          arr = this.columnsList.red_dizuo;
          break;
        case '65282':
          arr = this.columnsList.red_dizuo;
          break;
        case '65290':
          arr = this.columnsList.main_anquan;
          break;
        case '65292':
          arr = this.columnsList.main_anquan;
          break;
        case '65287':
          arr = this.columnsList.sensor;
          break;
        case '65286':
          arr = this.columnsList.dengguang;
          break;
        case '65285':
          arr = this.columnsList.dasuanli;
          break;
        default:
          arr = [];
      }
      return arr;
    },
  },
  methods: {
    // 改变
    changeModule(type) {
      this.currentType = type;
      this.postSearch();
    },
    // 下载日志
    downloadData() {
      getDownloadHistory({ ...this.postData }).then((res) => {
        console.log(res.data.result[0].url);
        window.open(res.data.result[0].url);
      });
    },
    onChange(date, dateString) {
      console.log(date, dateString);
      this.postData.start = dateString + ' 00:00:00';
      this.postData.end = dateString + ' 23:59:59';
    },
    handleChange(val) {
      console.log(val);
      this.postData.starlinkId = val;
    },
    postSearch() {
      this.allData = [];
      getHistoryStatus({
        moduleId: this.currentType,
        page: this.pagination.current,
        pagesize: this.pagination.pageSize,
        ...this.postData,
      }).then((res) => {
        console.log(res);
        if (res.data.total_num === 0) {
          this.allData = [];
        } else {
          this.allData = res.data.result;
        }
        this.pagination.total = res.data.total_num;
      });
    },
    // 改变分页
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination, filters, sorter);
      this.pagination = {
        ...pagination,
      };
      this.postSearch();
      // 传递参数获取最新数据
    },
    linkListInfo() {
      // 获取设备列表
      // 查询设备
      // 下载日志
      getStarlinkList().then((res) => {
        this.linkList = res.data.result;
      });
    },
  },
  mounted() {
    this.linkListInfo();
  },
};
</script>

<style lang="less" scoped>
.select-item {
  margin-right: 10px;
}
.btn-line {
  margin: 16px 0;
}
</style>
